﻿@using FFF.Test.Entity.FW.View;
@using Microsoft.AspNetCore.Http;
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Index_Layout.cshtml";
    ViewFWUser currentUserInfo = base.Context.Session.GetUserInfo();
    // 改
}
@if (currentUserInfo.IsHaveRights("FW_StandardRole", "00"))
{
    @section styles{
    }
    @section scripts{
        <script src="~/lib/vue/vue.min.js"></script>
        @*<script src="~/js/views/Main/Home/index.js?rnd=@DateTime.Now.Ticks"></script>*@
    }

    <div data-toggle="topjui-layout" data-options="fit:true">
        <div data-options="region:'center',title:'',fit:false,split:true,border:false">
            <table id="myDg"></table>

            <div id="myDg-toolbar" class="topjui-toolbar" data-options="
    grid:{
        type:'treegrid',
        id:'myDg',
        parentIdField:'pid'
        }">
                @if (currentUserInfo.IsHaveRights("FW_StandardRole", "01"))
                {
                    <a href="javascript:void(0)"
                       data-toggle="topjui-menubutton"
                       data-options="menu:'#addSubMenu',
                            hasDownArrow:true,
                            iconCls:'fa fa-plus'
            ">@L("新增")</a>

            <div id="addSubMenu" class="topjui-toolbar" style="width:120px;">
                <div data-toggle="topjui-menubutton"
                     data-options="iconCls:'fa fa-plus',onClick: function(){oepnAddDiag(true)}">
                    @L("新增根节点")
                </div>
                <div data-toggle="topjui-menubutton"
                     data-options="iconCls:'fa fa-plus',onClick: oepnAddDiag">
                    @L("新增子节点")
                </div>
            </div>
        }


                @if (currentUserInfo.IsHaveRights("FW_StandardRole", "03"))
                {
                    <a href="javascript:void(0)" data-toggle="topjui-menubutton" data-options="
                method:'doAjax',
                btnCls:'topjui-btn-delete',
                extend: '#myDg-toolbar',
                iconCls:'fa fa-trash',
                confirmMsg:'@L("确实删除选中的数据吗?")',
                grid: {uncheckedMsg:'@L("请先勾选要删除的数据")',param:'id:id'},
                url:'/api/services/app/FWStandardRole/BatchDelete' ">@L("删除")</a>
            }

                <a href="javascript:void(0)"
                   data-toggle="topjui-menubutton"
                   data-options="
            method:'filter',
            extend: '#myDg-toolbar',
            btnCls:'topjui-btn-filter'">@L("过滤")</a>

            @*<a href="javascript:void(0)"
                    data-toggle="topjui-menubutton"
                    data-options="
                method:'expandAll',
                extend: '#myDg-toolbar',
                iconCls:'fa fa-angle-down',
                btnCls:'topjui-btn-blue'">@L("展开全部")</a>

                <a href="javascript:void(0)"
                    data-toggle="topjui-menubutton"
                    data-options="method:
                'collapseAll',
                extend: '#myDg-toolbar',
                iconCls:'fa fa-angle-up',
                btnCls:'topjui-btn-blue'">@L("折叠全部")</a>*@

            <a href="javascript:void(0)"
               data-toggle="topjui-menubutton"
               data-options="
                btnCls:'topjui-btn-black',
                onClick:setRight">@L("设置权限")</a>

                @*<a href="javascript:void(0)"
                    data-toggle="topjui-menubutton"
                    data-options="
                        btnCls:'topjui-btn-black',
                        onClick:myQuery">@L("自定义方法")</a>*@
            </div>
        </div>
    </div>

    <!--自定义修改,尽量不要改框架的代码,修改全放这里,如果要修改框架代码,尽量在这里写个方法,在框架调用-->
    <script>
        var fWStandardRoleIndex = {
            queryObj: $.dm.getQueryObj(), // URL的参数
            buInfo: $.dm.getBuInfo(), // 门店信息
            userInfo: $.dm.getUserInfo(), // 用户信息
            appInfo: $.dm.getAppInfo(), // 系统信息
            selfClick: function () { // 自定义按钮对应的方法
                alert("123");
                // 选择一行
                $('#myDg').datagrid('selectRow', 1);
                // 提示信息
                //$.iMessager.alert('自定义方法', '自定义方法被执行了！', 'messager-info');

                //var checkedRows = $('#productDg').iDatagrid('getChecked');
                //console.log(checkedRows);

                //var options = $('#myDg').iDatagrid('options');
                //var selectedRow = $('#myDg').iDatagrid('getSelected');
                //console.log(selectedRow);

                // 提交参数查询表格数据
                //$('#productDg').iDatagrid('reload', {
                //    name: $('#name').iTextbox('getValue'),
                //    code: $('#code').iTextbox('getValue')
                //});
            }
        }
    </script>

    <!--全局变量和公共方法-->
    <script>

    // 处理添加和编辑的时候load之后调用的方法
    function addOrEditLoad(type,uuid) {
        if (type == "add") {
        }

        if (type == "edit") {
        }
    }

    // 保存时用户的自定义验证
    function userValidate() {
        //var myFormProductGUIDValue = $("#myFormProductGUID").iCombobox("getValue");
        //if (myFormProductGUIDValue.length != 36) {
        //    layer.msg("@L("请选择对应产品")");
        //    return false;
        //}

        return true;
    }
    </script>

    <!--表格操作-->
    <script>
    var dg = $('#myDg').iTreegrid({
        url: '/api/services/app/TreeGrid/GetTreeGird?viewName=view_FW_StandardRole&nodeName=StandardRoleName',
        fitColumns: true,
        expandUrl: '/api/services/app/TreeGrid/GetTreeGird?pid={id}&viewName=view_FW_StandardRole&nodeName=StandardRoleName',
        columns: [
            [
                {
                    field: 'id',
                    title: 'id',
                    checkbox: true,
                },
                { field: 'text', title: '@L("名称")', width: 120 },
                { field: 'code', title: '@L("层级编码")', width: 120 }
            ]
        ]
    });
    </script>


    <!--按钮相关-->
    <script>
    function oepnAddDiag(isRoot) {
        var selected = $('#myDg').iTreegrid('getSelected');
        if (!isRoot) {
            if (selected) {
                var result = {
                    parentGUID: selected.id
                };

            } else {
                layer.msg("@L("请选择一行作为子节点的父节点!")");
                return;
            }
        }
        var $editDialog = $('<form id="myFormDialog"></form>'); // 创建form表单元素
        $editDialog.iDialog({
            title: '@L("新增标准角色表(树)")',
            width: 750,
            height: 550,
            closed: false,
            cache: false,
            href: '/FW/FWStandardRole/FWStandardRoleEdit?mode=1',
            buttons: [{
                text: '@L("保存")',
                iconCls: 'fa fa-save',
                btnCls: 'topjui-btn-blue',
                handler: function () {
                    saveDate("saveAndClose");
                }
            },
            {
                text: '@L("关闭")',
                iconCls: 'fa fa-close',
                btnCls: 'topjui-btn-red',
                handler: function () {
                    $('#myFormDialog').iDialog('close');// 销毁dialog面
                }
            }],
            onLoad: function () {
                if (!isRoot) {
                    $editDialog.iForm('load', result);
                } else {
                    $("#divMyFormParentGUID").hide();
                }

                addOrEditLoad("add");
            }
        });
    }

    function openEditDiag() {
        var selectedRow = $('#myDg').iDatagrid('getSelected');
        var uuid = selectedRow.id;
        var $editDialog = $('<form id="myFormDialog"></form>'); // 创建form表单元素
        $editDialog.iDialog({
            title: '@L("编辑标准角色表(树)")',
            width: 750,
            height: 550,
            closed: false,
            cache: false,
            href: '/FW/FWStandardRole/FWStandardRoleEdit?uuid=' + uuid+'&mode=2',
            buttons: [{
                text: '@L("保存")',
                iconCls: 'fa fa-save',
                btnCls: 'topjui-btn-blue',
                handler: function () {
                    saveDate("saveAndClose");
                }
            },
            {
                text: '@L("关闭")',
                iconCls: 'fa fa-close',
                btnCls: 'topjui-btn-red',
                handler: function () {
                    $('#myFormDialog').iDialog('close');// 销毁dialog面
                }
            }],
            onLoad: function () {
                addOrEditLoad("edit");
                var data = { 'id': uuid };
                var options = {
                    url: '/api/services/app/FWStandardRole/GetViewDto',
                    maskJqSelect: '#myFormDialog',
                    data:data
                };

                var success = function (result) {
                    $editDialog.iForm('load', result);
                    $editDialog.find('[data-toggle="topjui-combobox"]').each(function (b) {
                        var c = $(this);
                        var d = getOptionsJson(c);
                        if (d.queryParams.viewName) {
                            c.iCombobox(d);
                        }
                    })
                }

                abp.ajax(options).done(success);
            }
        });
    }

    // operate表示操作类型有 save saveAndClose saveAndNew
    function saveDate(operate) {
        // 1. 显示进度条（提交时请使用进度条 或者 禁用'提交'按钮，防止因为网络延迟用户重复提交）
        $.iMessager.progress({ 'text': '@L("保存中……")' });
        var isValid = $('#myFormDialog').iForm('validate'); //  提交数据表单数据时进行表单验证
        var isUserValid = userValidate();
        if (!isValid || !isUserValid) {
            layer.msg("@L("格式不正确,请填写对应的必填项!")");
            $.iMessager.progress('close');  // 如果表单是无效的则隐藏进度条
            return;
        }

        var data = $.dm.parseQueryString($('#myFormDialog').serialize());
        var options = {
            url: '/api/services/app/FWStandardRole/CreateByDto',
            data: data
        };

        // 有ID就是更新
        if (data.id) {
            options.url = '/api/services/app/FWStandardRole/UpdateByDto'
        }
        var success = function (result) {
            $.iMessager.show({ title: '@L("我的消息")', msg: '@L("保存成功")', timeout: 2000, showType: 'slide' });// '消息窗口'组件
            $('#myFormDialog').iDialog('close');// 销毁dialog面板
            refreshTreeGrid();
        }
        abp.ajax(options).done(success);
    }

    function openDetailDiag(uuid) {
        var $editDialog = $('<form id="myDetailDialog"></form>'); // 创建form表单元素
        $editDialog.iDialog({
            title: '@L("查看")',
            width: 750,
            height: 550,
            closed: false,
            cache: false,
            href: '/FW/FWStandardRole/FWStandardRoleDetail?uuid=' + uuid,
            buttons: [{
                text: '@L("关闭")',
                iconCls: 'fa fa-close',
                btnCls: 'topjui-btn-red',
                handler: function () {
                    $('#myDetailDialog').dialog('close');// 销毁dialog面
                }
            }],
            onLoad: function () {
                var data = { 'id': uuid };
                var options = {
                    url: '/api/services/app/FWStandardRole/GetViewDto',
                    maskJqSelect: '#myDetailDialog',
                    data:data
                };

                var success = function (result) {
                    $("#FWStandardRoleDetailTableTemplate").tmpl(result)
                        .appendTo("#FWStandardRoleDetailTable");
                }
                abp.ajax(options).done(success);
            }
        });
    }

    // 删除行数据
    function deleteRow(id) {
        $.messager.confirm("@L("提示")", "@L("确定要删除该条数据吗?")", function (result) {
            if (result) {
                $.iMessager.progress({ 'text': '@L("处理中……")' });
                var data={'id':id};
                var options = {
                    url: '/api/services/app/FWStandardRole/DeleteByDto',
                    maskJqSelect: '#myDg'
                };

                var success = function (result) {
                    $("#myDg").iTreegrid("remove", id);
                    $.iMessager.show({ title: '@L("我的消息")', msg: '@L("删除成功")', timeout: 2000, showType: 'slide' });// '消息窗口'组件
                }

                abp.ajax(options).done(success);
            }
        })
    }

    // 自定义方法
    function myQuery() {
        alert("123");
        // 选择一行
        $('#myDg').datagrid('selectRow', 1);
        // 提示信息
        //$.iMessager.alert('自定义方法', '自定义方法被执行了！', 'messager-info');

        //var checkedRows = $('#productDg').iDatagrid('getChecked');
        //console.log(checkedRows);

        //var options = $('#myDg').iDatagrid('options');
        //var selectedRow = $('#myDg').iDatagrid('getSelected');
        //console.log(selectedRow);

        // 提交参数查询表格数据
        //$('#productDg').iDatagrid('reload', {
        //    name: $('#name').iTextbox('getValue'),
        //    code: $('#code').iTextbox('getValue')
        //});
    }
    function setRight() {
        var checkedRows = $('#myDg').iDatagrid('getSelected');
        var uuid = checkedRows.id;
        var $editDialog = $('<form id="mysetRightDialog"></form>'); // 创建form表单元素
        $editDialog.iDialog({
            title: '@L("设置权限")',
            width: 850,
            height: 550,
            closed: false,
            cache: false,
            href: '/FW/FWStandardRole2Action/SetRightIndex?standardRoleGUID=' + uuid,
            buttons: [{
                text: '@L("保存")',
                iconCls: 'fa fa-save',
                btnCls: 'topjui-btn-blue',
                handler: function () {
                    $.iMessager.progress({ 'text': '@L("保存中…")' });
                    var applicationGUID = $("#myFormApplicationGUID").val();
                    var chkActionIds = [];
                    var chkBox = $("#SetRightIndexTabs").find("input[name=rights]:checked");
                    $.each(chkBox, function (index, item) {
                        if (item.checked) {
                            chkActionIds.push(item.value);
                        }
                    });

                    var data = {
                        standardRoleGUID: uuid,
                        actionIds: chkActionIds,
                        applicationGUID: applicationGUID
                    }
                    var options = {
                        url: '/api/services/app/FWStandardRole2Action/BatchSaveRoleAction',
                        data: data
                    };

                    var success = function (result) {
                        $.iMessager.show({ title: '@L("消息")', msg: '@L("设置成功")', timeout: 2000, showType: 'slide' });// '消息窗口'组件
                        $('#mysetRightDialog').iDialog('close');// 销毁dialog面板
                    }
                    abp.ajax(options).done(success);
                }
            },
            {
                text: '@L("关闭")',
                iconCls: 'fa fa-close',
                btnCls: 'topjui-btn-close',
                handler: function () {
                    $('#mysetRightDialog').iDialog('close');// 销毁dialog面
                }
            }],
            onLoad: function () {
            }
        });
    }
    </script>
}
else
{
    <h1>@L("你没有该页面的权限")</h1>
}
